#include "lib_config.h"



#include "free-type/include/ft2build.h"
#include "free-type/include/freetype/freetype.h"
#include "databus.h"
#include "emodbus.h"
#include "emodbus_crc.h"
#include "lualib.h"
#include "lauxlib.h"
/*
 * AT指令测试
 * 指令错误
 * 不以AT开头\r\n结尾
 * AT指令超长
 * AT数据超长
 *
 *
 *
 */
//char *at_cmd={"AT+h\r\nAT+print=Hello!\r\nAT+Set=fafafaf\r\nAT+Set=abcdefghijk\r\nAT+Set=1234567890\r\n"};
char *at_cmd = {"AT+prin=1234567890\r\nAT+H=1234567890\r\nAT+LUA\r\n"};

#define NET_SHELF_BUFF_SIZE (1024*32)

unsigned char data_buff[NET_SHELF_BUFF_SIZE] = {
        //get_net_shelf_id
        0x00, 0x06, /* L */
        0x00, 0x01, /* T */
        0x00, 0xff, /* Value */

        //get_net_shelf_info
        0x00, 0x08, /* L */
        0x00, 0x02, /* T */
        0x00, 0xff, /* Value */
        0x00, 0xff, /* Value */

        //set_net_shelf_template
        0x00, 0x06, /* L */
        0x80, 0x01, /* T */
        /* Value
         * TVL
         * TVL
         * ...
         * */
        0x00, 0xff,

        //set_net_shelf_content
        0x00, 0x06, /* L */
        0x80, 0x02, /* T */
        0x00, 0xff, /* Value */

        //validate_set_net_shelf_led
//        0x00, 0x06, /* L */
//        0x81, 0x00, /* T */
//        0x00, 0xff, /* Value */
};

void net_shelf_test() {
    STORE_SHELF_STRUCT *store_shelf_and_cargo_struct;
    store_init();

    parse_cmd_from_buff(data_buff, NET_SHELF_BUFF_SIZE);
    store_show();
//    screen_draw_circle(50,50,50);
//    screen_draw_line(0,0,1000,1000);
//    screen_show();
    store_shelf_and_cargo_struct = get_store_shelf_and_cargo_struct();

    LOG("equal:%d\r\n", string_equal("aaa", "aaa"));
    LOG("equal:%d\r\n", string_equal("aaa", "aaaa"));
    LOG("string_to_double:%lf\r\n", string_to_double("3.1415926"));
    LOG("double_to_string:%s\r\n", double_to_string(3.1415));
    LOG("int_to_string:%s\r\n", int_to_string(1654864));
    LOG("string_to_int:%d\r\n", string_to_int("1654864"));
    LOG("float_to_string:%s\r\n", float_to_string(123.5555));
    LOG("size of STORE_SHELF_CARGO_STRUCT:%d\r\n", sizeof(STORE_SHELF_CARGO_STRUCT));
}

void data_trigger1(){
    printf("data_trigger1\r\n");
}

void data_bus_test(){
    DATA_BUS data={};
    data_bus_add_data("aaa",DATA_BUS_TYPE_INT,16,data_trigger1);
    data_bus_add_data("bbb",DATA_BUS_TYPE_INT,16,data_trigger1);
    data_bus_add_data("ccc",DATA_BUS_TYPE_INT,16,data_trigger1);

    printf("index:%d\r\n",data_bus_find_data_index_by_name("aaa"));
    printf("index:%d\r\n",data_bus_find_data_index_by_name("bbb"));
    printf("index:%d\r\n",data_bus_find_data_index_by_name("ccc"));
//    data_bus_change_data_by_index(0,"DDDDDD");
//    data_bus_change_data_by_index(0,"DDffDD");
//    data_bus_change_data_by_index(0,"DDffcc");
    data_bus_change_data_by_name("aaa","123");
    data_bus_change_data_by_name("aaa","456");
    data_bus_change_data_by_name("aaa","789");

    data=data_bus_get_data_by_name("aaa");
    printf("name:%s index:%d length:%d\r\n",data.name,data.index,data.length);

    data=data_bus_get_data_by_name("bbb");
    printf("name:%s index:%d length:%d\r\n",data.name,data.index,data.length);

    data=data_bus_get_data_by_name("ccc");
    printf("name:%s index:%d length:%d\r\n",data.name,data.index,data.length);

    printf("DATA:%s\r\n", data_bus_get_data_value_by_name("aaa"));
}
void emodbus_test(){
    //私有协议
    uint8_t databuff1[]={
            0x11,
            0x02,//私有功能码
            0xff,//功能码1
            0x50,//功能码2
            0x14,//crc
            0xe5,
    };
    exec_emodbus((uint8_t *)databuff1);

    //读数据
    uint8_t databuff2[]={
            0x11,//站号
            0x03,//功能码

            0x01,//地址
            0x90,

            0x00,//长度
            0x02,

            0x4a,//crc
            0xc7

    };
    exec_emodbus((uint8_t *)databuff2);

    //写数据
    uint8_t databuff3[]={
            0x11,//站号
            0x06,//功能码

            0x01,//地址
            0xf4,

            0x00,//长度
            0x02,

            0xa1,
            0xb2,

            0xc3,
            0xd4,

            0x40,//crc
            0xd5

    };
    exec_emodbus((uint8_t *)databuff3);


    //读数据
    uint8_t databuff4[]={
            0x11,//站号
            0x03,//功能码

            0x01,//地址
            0xf4,

            0x00,//长度
            0x02,

            0x95,//crc
            0x86

    };
    exec_emodbus((uint8_t *)databuff4);

    //读数据
    uint8_t databuff5[]={
            0x11,//站号
            0x03,//功能码

            0x01,//地址
            0x90,

            0x00,//长度
            0x02,

            0x4a,//crc
            0xc7

    };
    exec_emodbus((uint8_t *)databuff5);

}

int do_lua_script(){
    char str[]="printf('AAAAAAA')";
    lua_State *L;
    L = luaL_newstate(); 			//	创建Lua编译器
    luaopen_base(L);				//	注册基本函数
    luaL_dostring(L, str); 	//	执行脚本语句
    return 0;
}

int main(int argc, char **argv) {
//    net_shelf_test();
//    at_execute_cmds(at_cmd);
//    data_bus_test();
    emodbus_test();
//    lua_terminal(argc, argv);
//    at_execute_cmds(at_cmd);
//    data_bus_test();
//    lua_terminal(argc, argv);

//    do_lua_script();
    return 0;

}




